<?php

namespace app\controller\v1;

use app\model\OrderApprovalUserModel;
use app\model\OrderBomModel;
use app\model\OrderModel;
use app\validate\OrderValidate;

class Order extends System
{

    /**
     * Notes: 获取订单状态类型
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function statusList()
    {
        $data = OrderModel::STATUS_LIST;
        return $this->successJson($data);
    }


    /**
     * Notes: 添加订单
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function add()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('add')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->add($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();

    }


    /**
     * Notes: 编辑订单
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function edit()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('edit')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->edit($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();
    }

    /**
     * Notes: 订单详情
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('info')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->info($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }


    /**
     * Notes: 发起审批
     * User: chenLei
     * Date: 9/11/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function initiateApproval()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('initiateApproval')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }
        $orderModel = new OrderModel();
        $res = $orderModel->initiateApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);

    }


    /**
     * Notes: 撤销审批
     * User: chenLei
     * Date: 9/11/25
     * @return \think\response\Json
     */
    public function cancel()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('cancel')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->cancel($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);

    }

    /**
     * Notes: 待我审批的订单
     * User: chenLei
     * Date: 9/14/25
     * @return \think\response\Json
     */
    public function pendingApproval()
    {
        $orderModel = new OrderApprovalUserModel();
        $res = $orderModel->pendingApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }


    /**
     * Notes:  已审核的订单
     * User: chenLei
     * Date: 9/14/25
     * @return \think\response\Json
     */
    public function approved()
    {
        $orderModel = new OrderApprovalUserModel();
        $res = $orderModel->approved($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }

    /**
     * Notes: 提交审批
     * User: chenLei
     * Date: 9/14/25
     * @return \think\response\Json|void
     */
    public function submitApproval()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('submitApproval')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderApprovalUserModel = new OrderApprovalUserModel();
        $res = $orderApprovalUserModel->submitApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderApprovalUserModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 修改技术图纸
     * User: chenLei
     * Date: 9/15/25
     * @return \think\response\Json
     */
    public function updateTechnicalRequirements()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('updateTechnicalRequirements')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->updateTechnicalRequirements($this->params);
        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 采购部门
     * User: chenLei
     * Date: 9/16/25
     * @return \think\response\Json|void
     */
    public function bomListCg()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('bomList')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderBomModel = new OrderBomModel();
        $res = $orderBomModel->bomListCg($this->params);

        if ($res === false) {
            return $this->errorJson($orderBomModel->getError());
        }
        return $this->successJson($res);
    }


    /**
     * Notes: 生成采购单成功
     * User: chenLei
     * Date: 9/16/25
     * @return \think\response\Json
     */
    public function createCgOrder()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('createCgOrder')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderBomModel = new OrderBomModel();
        $res = $orderBomModel->createCgOrder($this->params);

        if ($res === false) {
            return $this->errorJson($orderBomModel->getError());
        }

        return $this->successJson();
    }
}